package cn.test;

import java.util.Random;

import static java.lang.Thread.sleep;

public class LighterController implements Runnable{
    private Road[] roads;
    private Lighter[] lighters;
    private final String[] lighterStatus = new String[]{"N2S", "N2E", "E2W", "E2S"};
    //每个step为1秒，就是每一秒车移动一次
    private int step;
    //duration表示经过多少step，灯的状态改变一次，默认为一秒变一次
    private int duration;
    //当前灯处于哪一个状态
    private int currentStatus;
    public LighterController() {
        new LighterController(1);
    }

    public LighterController(int duration) {
        //initial
        this.duration = duration;
        step = 0;
        //初始化时N2S灯为红
        currentStatus = 0;
        //以N,E,S,W顺序存储
        roads = new Road[4];
        for(int i=0;i<4;i++){
            roads[i] = new Road(i);
        }
        //以N2S,N2E,E2W,E2S顺序存储
        lighters = new Lighter[4];
        for(int j=0;j<4;j++){
            lighters[j] = new Lighter();
        }
        //首先是N2S灯为绿
        lighters[0].turnGreen();
    }
    public void nextStatus(){
        carMove();
        step = (step+1)%duration;
        if(step==duration-1) {
            turnLightsStatus();
        }
        //调试时可以不添加新的车辆
        addRandomCarsOnEveryRoads();
    }

    //每个周期，根据当前的Light状态，让对应的车移动
    private void carMove(){
        switch (currentStatus){
            case 0:
                letRightGo();
                roads[0].removeOneCarByForward(0);
                roads[2].removeOneCarByForward(0);
                break;
            case 1:
                letRightGo();
                roads[0].removeOneCarByForward(1);
                roads[2].removeOneCarByForward(1);
                break;
            case 2:
                letRightGo();
                roads[1].removeOneCarByForward(0);
                roads[3].removeOneCarByForward(0);
                break;
            case 3:
                letRightGo();
                roads[1].removeOneCarByForward(1);
                roads[3].removeOneCarByForward(1);
                break;
        }
    }
    private void turnLightsStatus(){
        lighters[currentStatus].turnRed();
        lighters[(currentStatus + 1) % 4].turnGreen();
        currentStatus = (currentStatus+1)%4;
    }

    //每一个周期都让每一条路上的右转车辆通过
    private void letRightGo(){
        for(int i=0;i<4;i++){
            roads[i].removeOneCarByForward(2);
        }
    }

    //每个周期都给每条路都添加1~2辆随机方向的车
    private void addRandomCarsOnEveryRoads(){
        Random r = new Random();
        for(int i=0;i<4;i++){
            for(int j=0;j<r.nextInt(3);j++) {
                roads[i].addRandomCar();
            }
        }
    }

    //打印当前每条路的状态
    private void print(){
        clearConsole();
        System.out.println("");
        String nineteenSpace = new String("                   ");
        String fifteenSpace = new String("            ");
        String fifteenSub = new String("-------------------");
        //print N2S
        System.out.println(lighterStatus[currentStatus]);
        for(int i=4;i>=0;i--){
            System.out.println(nineteenSpace+"|"+roads[0].getCarLineVertical(i)+"|"+fifteenSpace);
        }

        //print E2W && W2E
        System.out.println(fifteenSub+fifteenSpace+fifteenSub);
        StringBuilder[] right = roads[1].getCarLineHorizontal();
        StringBuilder[] left = roads[3].getCarLineHorizontal();
        for(int i=0;i<3;i++){
            System.out.println(left[i].toString()+fifteenSpace+right[i].toString());
        }
        System.out.println(fifteenSub+fifteenSpace+fifteenSub);

        //print S2N
        for(int i=0;i<5;i++){
            System.out.println(nineteenSpace+"|"+roads[2].getCarLineVertical(i)+"|"+nineteenSpace);
        }
    }

    private static void clearConsole() {
        try {
            Runtime.getRuntime().exec("cls");
        }
        catch (Exception exception) {
            //  Handle exception.
        }
    }


    @Override
    public void run() {
        while(true) {
            print();
            nextStatus();
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
